import cgi
import logging

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

from django.utils import simplejson

#

class State(db.Model):
    name = db.StringProperty()

class County(db.Model):
    name = db.StringProperty()
    state = db.ReferenceProperty(State,
                                 collection_name='counties')


# Inserts data. Use 
class MainPage(webapp.RequestHandler):
    def get(self):
        logger=logging.getLogger("my")
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write('<html>')
        self.response.out.write('<body>')

#        logger.debug('Inserting State Swietokrzyskie')
#        state = State(name='Swietokrzyskie')
#        state.put()
#        logger.debug('Inserting County Busko')
#        County(state=state,
#               name='Busko').put()
#        County(state=state,
#               name='Pinczow').put()
#       
#        state = State(name='Malopolskie')
#        state.put()
#        County(state=state,
#               name='Brzesko').put()
#        County(state=state,
#               name='Tarnow').put()

        logger.debug('Querying for counties of state Swietokrzyskie')
        q = State.all()
        q.filter("name =", "Swietokrzyskie")
        q.order("-name")
	
        self.response.out.write('<hr />')
	
        logger.debug("Type of q is %s" % type(q))
        state = q.get()
        
        logger.debug("Queried state %s" % str(state))
        for county in state.counties:
            self.response.out.write( "<br/>%s" % county.name)
            
        results = State.all()
        l = [] 

        for result in state.counties:
            d = {
                'name': result.name,
            }
            l.append(d)
        
        theresult_json = simplejson.dumps(l)
        
        logger.debug(theresult_json)

        self.response.out.write('</body>')
        self.response.out.write('</html>')
        
class JsonOutput(webapp.RequestHandler):
    def get(self):
        logger=logging.getLogger("my")
        logger.debug('JsonOutput')
        self.response.headers['Content-Type'] = 'text/plain'

        q = State.all()
        q.filter("name =", "Swietokrzyskie")
        q.order("-name")
    
        state = q.get()
        
        l = [] 

        for result in state.counties:
            d = {
                'name': result.name,
            }
            l.append(d)
        
        theresult_json = simplejson.dumps(l)
        
        logger.debug(theresult_json)

        self.response.out.write(theresult_json)
        

application = webapp.WSGIApplication(
                                     [('/counties/.*', MainPage),
                                     ('/counties-json/.*', JsonOutput)],
                                     debug=True)

def main():
    logger=logging.getLogger("my")  
    logger.setLevel(logging.DEBUG)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
    
    
